<?php
/**
 * 短信管理模块
 * @author chenhao 2009-01-11
 */
include( "inc/header.php" );
login_check( );
if ($_GET['Action']=='cltmoney') {
	if ($_SESSION['acl'] != 1) admin_showerrs( "<li>你无权查看短信余额</li>", null, 0 ); //@author by chenhao 2010-01-19 禁止普通管理员查看短信余额
	require_once('../inc/sms.php');
	$id = empty($_REQUEST['id']) ? 1 : $_REQUEST['id'];
	$db1->query("SELECT * FROM tun2_sms WHERE id=$id");
	if($db1->next_record()) {
		$user = $db1->Record['sms_user'];
		$password = $db1->Record['sms_password'];
		$sms = new sms($user,$password);
		$cltmoney = $sms->SelSum();
	}
?>
<table width="98%" border="1" align="center" cellpadding="5" cellspacing="0" class="table">
  <tr>
    <td>如果你遇到什么问题或困难，请与<a href="http://www.22cn.cn" target="_blank">呜雷科技</a>联系；如果此处出现<span style="color:red;">(-1，-2，-3)</span>时，请确保你的<span style="color:red;">短信帐户及密码正确</span>，你可以<a href="Admin_Sms_Set.php">修改帐号</a>确保短信能成功发送</td>
  </tr>
</table>
<br />
<table width="98%" border="1" align="center" cellpadding="5" cellspacing="0" class="table">
    <tr>
      <td width="35%" align="right">帐户剩余短信：</td>
      <td width="65%"><span style="color:red;"><?=sprintf('%d',$cltmoney)?></span> 条</td>
    </tr>
</table>
<?php
} elseif ($_GET['Action']=='phone') {
	
$_classArray = array(
			 0 => '企业员工',
			 1 => '公司领导',
			 2 => '合作伙伴'
);
function getClass($classID){
	global $_classArray;
	foreach ($_classArray as $key => $value) {
		if ($key == $classID) {
			return $value;
		}
	}
}
//添加手机号码
if($_GET['act']=='addphone'){
	$posts = $_POST;
	foreach ($posts as $key=>$value) {
		$posts[$key] = trim($value);
	}
	$class = $posts['phone_class'];
	$phone = $posts['phone_num'];
	$name  = empty($posts['phone_name']) ? '' : $posts['phone_name'];
	if (preg_match('/^(13[0-9]|15[0|1|2|3|6|7|8|9]|18[8|9])\d{8}$/',$phone)) {
		$query = $db->query("SELECT id FROM tun2_phone WHERE phone='{$phone}'");
		if ($db->num_rows($query)>0) {//表明该手机号已经存在
			admin_showerrs('你所添加的手机号码已经存在',null,0);
		} else {
			$addtime = time();
			$db->query("INSERT INTO tun2_phone (name,phone,class,acl,addtime) VALUES ('{$name}','{$phone}',{$class},1,{$addtime})");
			addlog("添加新手机号码", $_SESSION['user_name']);
		}
	}else {
		admin_showerrs('手机号码填写有误',null,0);
	}
}
//删除手机号码
if ($_GET['act']=='del') {
	if (isset($_POST['idarr'])){
		$chid = implode(",",$_POST['idarr']);
	}else{
		$chid = $_GET['id'];
	}
	if (!empty($chid)) { //解决多选时没有选择的情况
		$db->query( "DELETE FROM tun2_phone WHERE id IN (".$chid.")" );
		addlog("删除手机号码", $_SESSION['user_name']);
	}else {
		admin_showerrs('操作错误，请至少选择一个手机号码',null,0);
	}
}
//审核手机号
if($_GET['act']=='verify'){
	$db->query("SELECT acl FROM tun2_phone WHERE id='{$_GET[id]}'");
	if ($db->next_record()) {
		if ($db->Record['acl']==1) {
			$db->query("UPDATE tun2_phone SET acl=0 WHERE id='{$_GET[id]}'");
		}else {
			$db->query("UPDATE tun2_phone SET acl=1 WHERE id='{$_GET[id]}'");
		}
	}else {
		admin_showerrs('操作失败',null,0);
	}
}
?>
<table width="98%" border="1" align="center" cellpadding="5" cellspacing="0" class="table">
  <tr>
    <td>如果你遇到什么问题或困难，请与<a href="http://www.22cn.cn" target="_blank">呜雷科技</a>联系；</td>
  </tr>
</table>
<br />
<?php
//修改手机号码
if ($_GET['act']=='edit') {
	if (!empty($_GET['sb']) && $_GET['sb']=='sub') {
		$posts = $_POST;
		foreach ($posts as $key => $value) {
			$posts[$key] = trim($value);
		}
		$phone = $posts['phone_num'];
		$name  = $posts['phone_name'];
		$class = $posts['phone_class'];
		$id = $_GET['id'];
		if (preg_match('/^(13[0-9]|15[0|1|2|3|6|7|8|9]|18[8|9])\d{8}$/',$phone)) {
			$db->query("UPDATE tun2_phone SET name='{$name}',phone='{$phone}',class={$class} WHERE id=$id");
			addlog("修改手机号码", $_SESSION['user_name']);
			admin_showerrs('修改成功啦',null,1);
		} else {
			admin_showerrs('手机号码填写有误',null,0);
		}
	}
	$result = mysql_query("SELECT * FROM tun2_phone WHERE id='{$_GET[id]}'",$Conn);
	$row = mysql_fetch_array($result);
?>
<table width="98%" border="1" align="center" cellpadding="5" cellspacing="0" class="table">
<form id="addPhone" name="addPhoneForm" method="post" action="Admin_Sms.php?Action=phone&sb=sub&act=edit&id=<?=$_GET['id']?>">
    <tr>
      <td width="15%" align="right">
      <b>修改手机号码：</b></td>
      <td width="85%"><label>
        分组<select name="phone_class" id="phone_class">
		<?php
		foreach($_classArray AS $key => $value) {
		?>
          <option value="<?=$key?>" <?if($key==$row['class']){echo "selected=\"selected\"";}?>><?=$value?></option>
		  <?php } ?>
        </select>
       &nbsp;姓名
       <input name="phone_name" value="<?=$row['name']?>" type="text" style="width:80px;" id="phone_name" size="6" maxlength="10" />
       &nbsp;手机号码
       <input name="phone_num" type="text" value="<?=$row['phone']?>" style="width:140px;" id="phone_num" maxlength="11" />  
       <input type="submit" name="Submit" value="修 改" />
      </label></td>
    </tr>
	</form>
</table>
<?php
}else {//显示添加表单
?>
<table width="98%" border="1" align="center" cellpadding="5" cellspacing="0" class="table">
<form id="addPhone" name="addPhoneForm" method="post" action="Admin_Sms.php?Action=phone&act=addphone">
    <tr>
      <td width="15%" align="right">
      <b>添加手机号码：</b></td>
      <td width="85%"><label>
        分组<select name="phone_class" id="phone_class">
		<?php
		foreach($_classArray AS $key => $value) {
		?>
          <option value="<?=$key?>"><?=$value?></option>
		  <?php } ?>
        </select>
       &nbsp;姓名
       <input name="phone_name" type="text" style="width:80px;" id="phone_name" size="6" maxlength="10" />
       &nbsp;手机号码
       <input name="phone_num" type="text" style="width:140px;" id="phone_num" maxlength="11" />  
       <input type="submit" name="Submit" value="添 加" />
      </label></td>
    </tr>
	</form>
</table>
<?php
}
?>
<br />
<table width="98%" border="1" align="center" cellpadding="5" cellspacing="0" class="table">
  <form id="search" name="searchForm" method="post" action="Admin_Sms.php?Action=phone">
    <tr>
      <td width="15%" align="right"><b>搜索：</b></td>
      <td width="42%"><label>
      <input name="keywords" type="text" id="keywords" value="<?=$_POST['keywords']?>" maxlength="20" style="width:160px;" />
      <input type="submit" name="Submit2" value="搜 索" />
      </label></td>
      <td width="43%"><b>查看方式：</b><a href="Admin_Sms.php?Action=phone&amp;order=addtime">添加时间</a>&nbsp;&nbsp;&nbsp; <a href="Admin_Sms.php?Action=phone&amp;order=acl">审核</a></td>
    </tr>
  </form>
</table>
<br />
<table width="98%" border="1" align="center" cellpadding="5" cellspacing="0" class="table">
<form id="delarrayForm" name="delarrayForm" method="post" action="Admin_Sms.php?Action=phone&act=del">
  <tr style="font-weight:bolder;">
    <td>&nbsp;</td>
    <td width="65" align="center">ID</td>
    <td width="200" align="center">手机号码</td>
    <td width="130" align="center">姓名</td>
    <td width="130" align="center">分组</td>
    <td width="130" align="center">审核</td>
    <td width="182" align="center">添加时间</td>
    <td width="137" align="center">操作</td>
  </tr>
  <?php
  @$intPerpage = 20; //默认每页显示20条
  if ( $_GET['PB_page'] )
  {
  	$offset = ( $_GET['PB_page'] - 1 ) * $intPerpage;
  }
  else
  {
  	$offset = 0;
  }
  
  $listSql = "SELECT * FROM tun2_phone WHERE 1=1 ";
  $execc = "SELECT id FROM tun2_phone WHERE 1=1 ";
  if (isset($_POST['keywords'])) {
  	$listSql .= " AND name like '%{$_POST[keywords]}%' OR phone like '%{$_POST[keywords]}%'";
  	$execc .= " AND name like '%{$_POST[keywords]}%' OR phone like '%{$_POST[keywords]}%'";
  }
  if (isset($_GET['order']) && !empty($_GET['order'])) {
  	$listSql .= " ORDER BY $_GET[order] DESC LIMIT ".$offset.",{$intPerpage}";
  } else {
  	$listSql .= " ORDER BY addtime DESC LIMIT ".$offset.",{$intPerpage}";
  }
  $query = mysql_query($execc);
  $intNum = mysql_num_rows($query);
  $db->query($listSql);
  while($db->next_record()) {
  ?>
  <tr onmouseover="this.bgColor='#D3EDFC'" onmouseout="this.bgColor='#eff7fd'">
    <td align="center" width="40">
        <input type="checkbox" name="idarr[]" id="idarr" value="<?=$db->Record['id']?>" />    </td>
    <td align="center"><?=$db->Record['id']?></td>
    <td><?=$db->Record['phone']?></td>
    <td align="center"><?=$db->Record['name']?></td>
    <td align="center"><?=getClass($db->Record['class'])?></td>
    <td align="center"><a href="Admin_Sms.php?Action=phone&act=verify&id=<?=$db->Record['id']?>"><img src="images/<? if($db->Record['acl']==1) {echo "icon_01.gif";}else{echo "icon_02.gif";}?>" border="0" /></a></td>
    <td align="center"><?=date('Y-m-d H:s:i',$db->Record['addtime'])?></td>
    <td align="center"><a href="Admin_Sms.php?Action=phone&amp;act=edit&amp;id=<?=$db->Record['id']?>">修改</a>&nbsp; <a href="Admin_Sms.php?Action=phone&amp;act=del&amp;id=<?=$db->Record['id']?>" onclick="delcfm();">删除</a></td>
  </tr>
  <?php
  }
  ?>
  <tr>
    <td align="center">&nbsp;</td>
    <td colspan="7"><label>
      <input type="submit" name="delarr_button" value="批量删除" />
    </label></td>
  </tr>
  <tr>
    <td height="40" colspan="8" align="center">
    <?php
    //分页类
    require_once( "../inc/class.page.php" );
    class mypage extends page
    {

    	public function show( )
    	{
    		$_showpage = "<div class=\"pagenavi\" id=\"lopage\">";
    		$_showpage .= $this->first_page( )." ";
    		$_showpage .= $this->nowbar( "", "curr" );
    		$_showpage .= "<span class=\"break\">...</span>";
    		$_showpage .= $this->last_page( );
    		$_showpage .= "   (总计<span class=\"num\">".$this->totalpage."</span>页 <span class=\"num\">".$this->total."</span>条记录)</div>";
    		$_showpage .= "</div>";
    		return $_showpage;
    	}

    }
    $page = new mypage( array(
    "total" => $intNum,
    "perpage" => $intPerpage
    ) );
    echo $page->show( );
    ?></td>
    </tr>
</form>  
</table>
<?php
}
?>
